package org.example.shoppingmall.util;

import java.io.InputStream;
import java.sql.*;
import java.util.Properties;

public class JDBCUtil {
    private static String jdbcUri, jdbcUsername, jdbcPassword, jdbcClassName;
    private static JDBCUtil jdbcUtil = null;

    static
    {
        try
        {
            InputStream is = JDBCUtil.class.getClassLoader().getResourceAsStream("jdbc.properties");
            Properties prop = new Properties();
            prop.load(is);
            jdbcClassName = prop.getProperty("jdbc.className");
            jdbcUsername = prop.getProperty("jdbc.username");
            jdbcPassword = prop.getProperty("jdbc.password");
            jdbcUri = prop.getProperty("jdbc.uri");
            Class.forName(jdbcClassName);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    public static Connection getConn() throws SQLException
    {
        return DriverManager.getConnection(jdbcUri, jdbcUsername, jdbcPassword);
    }

    public static void release(ResultSet rs, PreparedStatement ps, Connection conn) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
